import { vHandle } from "./base_vue.js"
import attrsCheck from "../general/attrsCheck.js"

function base_control() {
    this.context = undefined
    this.constructor_template = {} // constructor template.
    this.compile_file = undefined
    this.vnode = {} // vNode template.
    this.render_ops = {} // render options template.
}

base_control.prototype = new vHandle();
base_control.prototype.constructor = base_control;
base_control.prototype.build = function (ops) {
    return (new attrsCheck(ops, ["constructor", "render"])).then((data) => {
        return this.create(data)
    }).catch((msg) => {
        throw msg
    })
}
base_control.prototype.create_instance = function (data) {
    for (let o in data) {
        if (o.indexOf('props_') > -1) {
            if (!('props' in this.constructor_template)) {
                this.constructor_template['props'] = {}
            }
            if (!(o.replace('props_', "") in this.constructor_template.props)) {
                let props_type = typeof (data[o])
                switch (props_type) {
                    case "string":
                        {
                            this.constructor_template.props[o.replace('props_', "")] = { String, default: () => undefined }
                            break
                        }
                    case "object": {
                        if (Array.isArray(data[o])) {
                            this.constructor_template.props[o.replace('props_', "")] = { Array, default: () => [] }
                        }
                        else {
                            this.constructor_template.props[o.replace('props_', "")] = { Object, default: () => { } }
                            break
                        }
                    }
                    case "number":
                        {
                            this.constructor_template.props[o.replace('props_', "")] = { Number, default: () => undefined }
                            break
                        }
                    case "boolean":
                        {
                            this.constructor_template.props[o.replace('props_', "")] = { Boolean, default: () => false }
                            break
                        }
                    default:
                        {
                            //throw new Error('props type is error.')
                        }
                }
            }
            this.render_ops.propsData[o.replace("props_", "")] = data[o]
        }
        else {
            let tmpdata = Object.assign({}, this.constructor_template.data())
            tmpdata[o.replace("data_", "")] = data[o]
            this.constructor_template.data = () => {
                return tmpdata
            }
        }
    }
}
base_control.prototype.render_control = function (vNode = undefined, compile_file = undefined) {
    this.buildConstructor(this.constructor_template, vNode, this.context)
    return this.instance(this.render_ops)
};

base_control.prototype.create = function (ops) {
    this.constructor_template = ops.constructor
    this.render_ops = ops.render
    this.vnode = ops.hasOwnProperty("vnode") ? ops.vnode : undefined
    // if ops has 'compile_file' then compile source.
    this.compile_file = ops.hasOwnProperty('compile_file') ? ops.compile_file : undefined
    if ('func' in ops.render) {
        try {
            //add event function.
            let rootPath = process.cwd()
            let requirePath = rootPath + ops.render.func
            if (!('methods' in this.constructor_template)) {
                this.constructor_template['methods'] = {}
            }
            //this.constructor_template['methods']["tirggers"] =(event)=>{test()}
        }
        catch (error) {
            throw error
        }
    }
    if (this.constructor_template != null && this.render_ops != null && this.vnode != null) {
        return this.render_control(this.vnode, this.compile_file)
    }
    else {
        throw new ReferenceError(`[ERROR] control prototype 'contructor_template','render_ops' can not null. ${this}`)
    }
}

export default base_control